<template>

  <div class="home-content">
    <HomeTitle category="一级模块管理" variety="一级模块列表" class="title"></HomeTitle>
    <el-row>
      <el-button type="primary" size='small' data-status="add" @click="addModule">新增一级模块</el-button>
    </el-row>
    <div class="mt20">
      <el-table :data="moduleList" border class="widthp100 cursor">
        <el-table-column prop="name" width="100" label="模块名称" align="center"></el-table-column>
        <el-table-column prop="sort" width="100" label="权重" align="center"></el-table-column>
        <el-table-column prop="url" width="" label="路由" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button @click="addTwoModule(scope.$index, scope.row)" type="text" size="small">二级模块</el-button>
            <el-button @click="handleEdit(scope.$index, scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="新增模块" :visible.sync="dialog.show" width="360px" :center="true" :append-to-body="true">
      <el-form label-width="80px" size="medium">
        <div class="prow10">
          <el-form-item class="mt20" label="模块名称">
            <el-input v-model="listInfo.name" placeholder="请输入模块名称"></el-input>
          </el-form-item>
          <el-form-item class="mt15" label="权重">
            <el-input v-model="listInfo.sort" placeholder="请输入权重"></el-input>
          </el-form-item>
          <el-form-item class="mt15" label="路由链接">
            <el-input v-model="listInfo.url" placeholder="请输入路由链接"></el-input>
          </el-form-item>
          <el-form-item class="mt15" v-if="addEdit" label="是否禁用">
            <el-select v-model="listInfo.disable" placeholder="请选择是否禁用">
              <el-option label="禁用" value=true>禁用</el-option>
              <el-option label="启用" value=false>启用</el-option>
            </el-select>
          </el-form-item>
          <div slot="footer" class="dialog-footer tar mt20">
            <el-button @click="dialog.show = false" size="small">取 消</el-button>
            <el-button type="primary" @click="confirmAddEdit" size="small">确 定</el-button>
          </div>
          <!-- <div class="dialog-footer mt30 tac widthp100 block flex-justify prow10 boxsizing dialog-footer-btn">
            <el-button class="widthp100 pcol10" type="primary" @click="confirmReturnDinner" size="medium" round>确 定</el-button>
          </div> -->
        </div>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
  import {moduleList} from '@/request/api'; //
  import {addModule} from '@/request/api'; //
  import {updateModule} from '@/request/api'; //
  import {deleteModule} from '@/request/api'; //

  /* import qs from 'qs'; */
  let listInfo = {
    name: '',
    parentId: 0,
    sort: '',
    url: '',
    id:'',
    disable:''
  };
  export default {
    name: 'module',
    data: function() {
      return {
        dialog: {
          show: false
        },
        currentPage: 1,
        listInfo: listInfo,
        params: {
          limit: 10,
          pagesize: 0,
          parentId: ''
        },
        moduleList: [],
        addEdit: false
      }
    },
    mounted: function() {
      let params = this.$route.query; //parms包含传递的所有参数
      console.log(params);
    },
    created: function() {
      let params = this.$route.query;
      console.log(params.parentId);
      //this.params.parentId=params.parentId;
      this.getModuleList();
    },
    methods: {
      /*添加*/
      addModule: function(e) {
        let status = e.currentTarget.dataset.status;
        console.log(status);
        this.addEdit = false;
        this.dialog.show = true;

      },
      /*编辑*/
      handleEdit(index, row) {
        console.log(index, row);
        this.addEdit = true;
        this.dialog.show = true;
        this.listInfo=JSON.parse(JSON.stringify(row));
        this.listInfo.disable = this.listInfo.disable ? '禁用' : '启用';
      },
      /* 确认添加或者编辑 addEdit:true=>编辑 false=>添加*/
      confirmAddEdit: function() {
        if (!this.addEdit) {
          addModule(this.listInfo).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.dialog.show = false;
              this.getModuleList();
            };
            //this.leftMenuTrees = res.data;
          });
        } else {
          let params=JSON.parse(JSON.stringify(this.listInfo));
          params.disable=params.disable==='true'?true:false;

          updateModule(params).then(res => {
            console.log(res);
            if(res.code==200){
              this.dialog.show = false;
              this.$common.message('编辑成功！');
              this.getModuleList();
            }else{
              this.$common.message(res.message);
              this.dialog.show = false;
            }
            //this.leftMenuTrees = res.data;
          });
        };

      },
      /* 删除*/
      handleClick(row) {
        console.log(row);
        deleteModule({id:row.id}).then(res => {
          console.log(res);
          if (res.code === 200) {
            this.moduleList = res.data.data;
          };
        });
      },
      /* 新增二级模块*/
      addTwoModule(index, row){
        console.log(index, row);
        let parentId=row.id;
        let url='/moduleitem'
        this.$router.push({
          path: url,
          query: {
            parentId: parentId
          }
        });
      },
      getModuleList() {
        moduleList(this.params).then(res => {
          console.log(res);
          if (res.code === 200) {
            this.moduleList = res.data.data;
          };
        });
      }
    }

  }
</script>

<style>

</style>
